#!/bin/bash

export LANG=en_US

declare -a FNAME1 FNAME2 LINES

RPATH=`echo $0 | awk '{ match($1,"testxs"); print substr($1,1,RSTART-1); }'`

FILE1=#___xs_comparison___#

CNT=0
echo -n "" > $FILE1
while read LINE; do
  FNAME1[CNT]=`echo $LINE | awk '{ print $1; }'`
  FNAME2[CNT]=`echo $LINE | awk '{ print $2; }'`
  if [ "${FNAME1[CNT]}" != "" ]; then 
    LINES[CNT]="$LINE"
    if (( CNT < 4 )); then
      echo "$LINE" >> $FILE1
      (( ++CNT ))
    elif [ "${FNAME2[CNT]}" != "" ]; then
      (( ++CNT ))
    fi
  fi
done
OFILE=${FNAME1[0]}
if ! test -d $OFILE; then 
  mkdir $OFILE
fi
for (( i=4; i<$CNT; ++i )); do
  if test -f ${FNAME1[i]}; then
    echo "%% $i" >> $FILE1
    cat ${FNAME1[i]} >> $FILE1
  else
    if [ "${FNAME1[i]}" = "#" ]; then
      echo "${LINES[i]}" >> $FILE1
    fi
  fi
done
echo "end" >> $FILE1
for (( i=4; i<$CNT; ++i )); do
  if test -f ${FNAME2[i]}; then
    echo "%% $i" >> $FILE1
    cat ${FNAME2[i]} >> $FILE1
  else
    if [ "${FNAME1[i]}" = "#" ]; then
      echo "${LINES[i]}" >> $FILE1
    fi
  fi
done
echo "end" >> $FILE1

awk -f $RPATH"testxs.awk" < $FILE1

rm $FILE1

Compare -p$RPATH -f"testxs.cmp" -okb -W$OFILE -D./ \
	-P$OFILE/ -T"FNAME $OFILE.xsd.dat"
